package com.cinc.ebc.mm.util;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.config.AutoPoiDictConfig;
import org.jeecgframework.core.util.ApplicationContextUtil;
import org.jeecgframework.poi.excel.annotation.Excel;
import org.jeecgframework.poi.excel.annotation.ExcelVerify;
import org.jeecgframework.poi.excel.entity.result.ExcelVerifyHanlderResult;
import org.jeecgframework.poi.excel.imports.verifys.BaseVerifyHandler;
import org.jeecgframework.poi.handler.inter.IExcelVerifyHandler;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;

/**
 * excel校验处理工具
 *
 * @author wangxing
 * @date 2022/7/17 18:26
 */
@Slf4j
public class ExcelVerifyHandlerUtil {


    /**
     * Excel导入基本校验：校验空值、字典项，不通过则抛出异常
     *
     * @param countObj
     * @param fieldNameList
     * @param excelNameList
     * @return
     */
    public static IExcelVerifyHandler getExcelVerifyHandlerBase(int[] countObj, List<String> fieldNameList, List<String> excelNameList) {
        IExcelVerifyHandler excelVerifyHandler = new IExcelVerifyHandler() {
            @Override
            public String[] getNeedVerifyFields() {
                return new String[0];
            }

            @Override
            public void setNeedVerifyFields(String[] strings) {

            }

            @Override
            public ExcelVerifyHanlderResult verifyHandler(Object o, String s, Object o1) {
                countObj[0] = countObj[0] + 1;
                //行号
                int rowNum = countObj[0];
                //列号
                int colNum = excelNameList.indexOf(s) + 1;

                Field field = null;
                try {
                    field = o.getClass().getDeclaredField(fieldNameList.get(colNum - 1));
                } catch (NoSuchFieldException e) {
                    throw new RuntimeException("导入校验异常");
                }
                //基本规则校验
                ExcelVerify excelVerify = field.getAnnotation(ExcelVerify.class);
                if (excelVerify != null) {
                    if (excelVerify.notNull() && oConvertUtils.isEmpty(o1)) {
                        throw new RuntimeException(s + "不能为空，行列【" + rowNum + "," + colNum + "】");
                    }
                    if (excelVerify.isEmail()) {
                        ExcelVerifyHanlderResult emailVerifyResult = BaseVerifyHandler.isEmail(s, o1);
                        if (!emailVerifyResult.isSuccess()) {
                            throw new RuntimeException(emailVerifyResult.getMsg() + "，行列【" + rowNum + "," + colNum + "】");
                        }
                    }
                    if (excelVerify.isMobile()) {
                        ExcelVerifyHanlderResult emailVerifyResult = BaseVerifyHandler.isMobile(s, o1);
                        if (!emailVerifyResult.isSuccess()) {
                            throw new RuntimeException(emailVerifyResult.getMsg() + "，行列【" + rowNum + "," + colNum + "】");
                        }
                    }
                    if (excelVerify.isTel()) {
                        ExcelVerifyHanlderResult emailVerifyResult = BaseVerifyHandler.isTel(s, o1);
                        if (!emailVerifyResult.isSuccess()) {
                            throw new RuntimeException(emailVerifyResult.getMsg() + "，行列【" + rowNum + "," + colNum + "】");
                        }
                    }
                    if (excelVerify.maxLength() != -1) {
                        ExcelVerifyHanlderResult emailVerifyResult = BaseVerifyHandler.maxLength(s, o1, excelVerify.maxLength());
                        if (!emailVerifyResult.isSuccess()) {
                            throw new RuntimeException(emailVerifyResult.getMsg() + "，行列【" + rowNum + "," + colNum + "】");
                        }
                    }
                    if (excelVerify.minLength() != -1) {
                        ExcelVerifyHanlderResult emailVerifyResult = BaseVerifyHandler.minLength(s, o1, excelVerify.minLength());
                        if (!emailVerifyResult.isSuccess()) {
                            throw new RuntimeException(emailVerifyResult.getMsg() + "，行列【" + rowNum + "," + colNum + "】");
                        }
                    }
                    if (oConvertUtils.isNotEmpty(excelVerify.regex())) {
                        ExcelVerifyHanlderResult emailVerifyResult = BaseVerifyHandler.regex(s, o1, excelVerify.regex(), excelVerify.regexTip());
                        if (!emailVerifyResult.isSuccess()) {
                            throw new RuntimeException(emailVerifyResult.getMsg() + "，行列【" + rowNum + "," + colNum + "】");
                        }
                    }
                }
                //字典校验
                Excel excel = field.getAnnotation(Excel.class);
                if (oConvertUtils.isNotEmpty(excel.dicCode())) {
                    AutoPoiDictConfig poiDictConfig = ApplicationContextUtil.getContext().getBean(AutoPoiDictConfig.class);
                    String[] textAndValueArr = poiDictConfig.queryDict(excel.dictTable(), excel.dicCode(), excel.dicText());
                    if (textAndValueArr == null || textAndValueArr.length == 0) {
                        throw new RuntimeException(s + "字典项未定义");
                    }
                    long valueCount = Arrays.stream(textAndValueArr).filter(textAndValue -> textAndValue.indexOf("_" + o1.toString()) >= 0).count();
                    if (valueCount == 0) {
                        throw new RuntimeException(s + "无法识别，行列【" + rowNum + "," + colNum + "】");
                    }
                }

                return new ExcelVerifyHanlderResult(true);
            }
        };

        return excelVerifyHandler;
    }

}